package com.lczyfz.istep.modules.openstack.api.neutron.impl.securitygroup;

/**
 * ��ȫ�����ӿڵ�ʵ����
 * @author clq
 *
 */

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.securitygroup.SecuritygroupRules;
import com.lczyfz.istep.modules.openstack.api.neutron.model.securitygroup.SecuritygroupRule;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class SecuritygroupRuleImpl implements SecuritygroupRules {
	public RequestFactory factory = new RequestFactory("neutron");
	
	@Override
	public List<SecuritygroupRule> getSecuritygroupRules() {
		List<SecuritygroupRule> securitygrouprules = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-group-rules");
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}	
		
		securitygrouprules = NeutronParse.resolveSecuritygroupRules(body);
		return securitygrouprules;
	}

	@Override
	public SecuritygroupRule getSecuritygroupRuleDetail(String srId) {
		SecuritygroupRule securitygrouprule = new SecuritygroupRule();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-group-rules/" + srId);
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		securitygrouprule = NeutronParse.resolveSecuritygroupRuleDetail(body);
		return securitygrouprule;
	}

	@Override
	public SecuritygroupRule createSecuritygroupRule(SecuritygroupRule srinput) {
		SecuritygroupRule securitygrouprule = new SecuritygroupRule();
		
		JSONObject params = NeutronParse.packageSecuritygroupRule(srinput);
		System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-group-rules");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		
		Response results = HTTP.getConnectionResult(request);	
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		securitygrouprule = NeutronParse.resolveSecuritygroupRuleDetail(res);
		return securitygrouprule;
	}

	@Override
	public void deleteSecuritygroupRule(String srId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-group-rules/" + srId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
		
	}

	public static void main(String[] args){
		SecuritygroupRuleImpl srtest = new SecuritygroupRuleImpl();
//		System.out.println("��ȫ�������Ϣ");
//		List<SecuritygroupRule> srlist = srtest.getSecuritygroupRules();
//		for(SecuritygroupRule sr : srlist){
//			System.out.println(sr.getSrId()+" "+sr.getSrDirection() + " "+sr.getSrEthertype()+" "+sr.getSrProtocol()+" "+sr.getSrRemoteGroupId()+" "+sr.getSrRemoteIpPrefix()+ " "+sr.getSrSecurityGroupId());
//		}
//		
//		System.out.println("��ȫ�������ϸ��Ϣ");
//		SecuritygroupRule sr = srtest.getSecuritygroupRuleDetail("3ffa8793-ad82-492e-bf64-56cad0adfb73");
//		System.out.println(sr.getSrId()+" "+sr.getSrDirection() + " "+sr.getSrEthertype()+" "+sr.getSrProtocol()+" "+sr.getSrRemoteGroupId()+" "+sr.getSrRemoteIpPrefix()+ " "+sr.getSrSecurityGroupId());
//	
		
		System.out.println("������ȫ�����");
		
		SecuritygroupRule srtt = new SecuritygroupRule();
		srtt.setSrDirection("egress");
		srtt.setSrEthertype("IPv6");
		srtt.setSrProtocol("tcp");
		srtt.setSrSecurityGroupId("7a26baf5-3a97-4bc4-aba2-8eafd4f7d94f");
		srtt.setSrRemoteGroupId("4afff3cf-68a2-4240-83e9-423df66e995b");
		
		SecuritygroupRule sr_new = srtest.createSecuritygroupRule(srtt);
		System.out.println(sr_new.getSrId()+" "+sr_new.getSrDirection() + " "+sr_new.getSrEthertype()+" "+sr_new.getSrProtocol()+" "+sr_new.getSrRemoteGroupId()+" "+sr_new.getSrRemoteIpPrefix()+ " "+sr_new.getSrSecurityGroupId());

	//	System.out.println("ɾ����ȫ�����");
	//	srtest.deleteSecuritygroupRule("763bda63-c9fa-42c3-b646-953aa0feddf2 ");
		
	}
	
	
}
